package com.bz.xtcx.manager.task;

import com.bz.xtcx.manager.entity.CmsMeeting;
import com.bz.xtcx.manager.mapper.CmsMeetingMapper;
import com.bz.xtcx.manager.persist.conditions.MeetingCondition;
import com.bz.xtcx.manager.utils.WzDateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.Destination;
import java.util.Date;
import java.util.List;

@Component
public class CmsMeetingTask {

    /** 日志. */
    private final Logger logger = LoggerFactory.getLogger(CmsMeetingTask.class);

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private CmsMeetingMapper cmsMeetingMapper;

    @Autowired
    private Destination reqMeetingEmailQueue;

    @Autowired
    private Destination reqMeetingMobileQueue;

    @Autowired
    private Destination respMeetingEmailQueue;

    @Autowired
    private Destination respMeetingMobileQueue;

    /**
     * 将超时未回复的沟通申请设为超时并通知.
     */
    @Scheduled(cron = "0 */30 * * * ?")
    public void cleanOvertimeNegotiationRequest() {
        logger.info("超时对接清理开始");
        // 查询三天前发起的并没有回复的请求
        Date lostStartTime = WzDateUtil.afterNDays(new Date(), -3);
        Date nowDate = new Date();
        MeetingCondition mcVo = new MeetingCondition();
        mcVo.setSubmitTimeEnd(lostStartTime);
        List<CmsMeeting> meetingList = null;
        try {
            meetingList = cmsMeetingMapper.selectOvertimeByCondition(mcVo);
        } catch (Exception e) {
            logger.error("查询待处理列表发生异常.", e);
        }
        if (null == meetingList || 0 == meetingList.size()) {
            return;
        }
        CmsMeeting updVo = null;
        // 循环处理过期
        for (CmsMeeting cmData : meetingList) {
            try {
                updVo = new CmsMeeting();
                updVo.setId(cmData.getId());
                updVo.setCheckStatus(3);
                updVo.setCheckContent("已过期");
                updVo.setCheckTime(nowDate);
                updVo.setUpdater("auto");
                int updCnt = cmsMeetingMapper.updateByPrimaryKeySelective(updVo);
                // 更新状态后发送通知消息
                if (1 == updCnt) {
                    jmsMessagingTemplate.convertAndSend(respMeetingMobileQueue, cmData);
                    jmsMessagingTemplate.convertAndSend(respMeetingEmailQueue, cmData);
                }
            } catch (Exception e) {
                logger.error("处理对接申请[" + cmData.getCode() + "]超时处理时发生异常.", e);
            }
        }
        logger.info("超时对接清理结束");
    }

    /**
     * 将所有未发送成功的重新发送.
     */
//    @Scheduled(cron = "0 */15 * * * ?")
    public void resendMeetingReq() {
        logger.info("对接通知处理开始");
        Date nowDate = new Date();
        List<CmsMeeting> meetingList = null;
        try {
            meetingList = cmsMeetingMapper.selectNoticeErr();
        } catch (Exception e) {
            logger.error("查询待处理列表发生异常.", e);
        }
        if (null == meetingList || 0 == meetingList.size()) {
            return;
        }
        CmsMeeting updVo = null;
        for (CmsMeeting cmData : meetingList) {
            try {
                if (-1 == cmData.getCheckStatus()) {
                    updVo = new CmsMeeting();
                    updVo.setId(cmData.getId());
                    updVo.setCheckStatus(0);
                    updVo.setCheckTime(nowDate);
                    int updCnt = cmsMeetingMapper.updateByPrimaryKeySelective(updVo);
                    if (1 == updCnt) {
                        if (0 == cmData.getReqMobileStatus() && 3 >= cmData.getReqMobileCnt()) {
                            jmsMessagingTemplate.convertAndSend(reqMeetingMobileQueue, cmData);
                        }
                        if (0 == cmData.getReqMailStatus() && 3 >= cmData.getReqMailCnt()) {
                            jmsMessagingTemplate.convertAndSend(reqMeetingEmailQueue, cmData);
                        }
                    }
                } else if (0 == cmData.getCheckStatus()) {
                    if (0 == cmData.getReqMobileStatus() && 3 >= cmData.getReqMobileCnt()) {
                        jmsMessagingTemplate.convertAndSend(reqMeetingMobileQueue, cmData);
                    }
                    if (0 == cmData.getReqMailStatus() && 3 >= cmData.getReqMailCnt()) {
                        jmsMessagingTemplate.convertAndSend(reqMeetingEmailQueue, cmData);
                    }
                } else if (1 == cmData.getCheckStatus() || 2 == cmData.getCheckStatus() || 3 == cmData.getCheckStatus()) {
                    if (0 == cmData.getRespMobileStatus() && 3 >= cmData.getRespMobileCnt()) {
                        jmsMessagingTemplate.convertAndSend(respMeetingMobileQueue, cmData);
                    }
                    if (0 == cmData.getRespMailStatus() && 3 >= cmData.getRespMailCnt()) {
                        jmsMessagingTemplate.convertAndSend(respMeetingEmailQueue, cmData);
                    }
                }
            } catch (Exception e) {
                logger.error("处理对接申请[" + cmData.getCode() + "]发送通知时发生异常.", e);
            }
        }
        logger.info("对接通知处理结束");
    }
}
